// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    username:"",
    password:"",
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
      //登录接口
      getLogin(){
        wx.request({
          url: 'http://admin.bsmall.byesame.com/wx/auth/login',
          data:{
            password:this.data.password,
            username:this.data.username
          },
          method:"POST",
          success:(res)=>{
            console.log("这是成功的输出",res);
            if (res.data.errno ==0) {
                wx.setStorageSync('token', res.data.data.token)
                wx.switchTab({
                    url: '/pages/choiceness/choiceness',
                  })  
            }else{
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'error'
                })
            }
          },
          fail:(err)=>{
            console.log("这是失败的输出",err);
          }
        })
    },
    // 跳转到忘记密码
    toForget(){
        wx.navigateTo({
          url: '../forget/forget',
        })
    },

    //跳转到免费注册
    toRegister(){
        wx.navigateTo({
            url: '../register/register',
          })
    },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})
